package com.leo.blog.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @Classname JsonUtils
 * @Description TODO
 * @Date 2023/4/13 9:43
 * @Created by XiongXiong
 * @Author: X.I.O
 */
public class JsonUtils {

    public static String getString(Object obj)  {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String str=objectMapper.writeValueAsString(obj);
            return str;
        }catch (JsonProcessingException e){
            e.printStackTrace();
        }
        return null;
    }

    public static <T> T strToObj(String str,Class<T> clazz){
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            T t=objectMapper.readValue(str,clazz);
            return t;
        }catch (JsonProcessingException e){
            e.printStackTrace();
        }
        return null;
    }
}
